home *** CD-ROM | disk | FTP | other *** search
/ World of Sound / World of Sound.iso / utils / miditools / kawaied / src / funcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-31  |  27.7 KB  |  1,139 lines

  1. #include <exec/memory.h>
  2. #include "inc.h"
  3. #include "defs.h"
  4. #include "req_manx_5.c"
  5. #include "zz_pointer.h"
  6.  
  7. #include <devicetoolkits/input.h>
  8. #include <deviceToolKits/proto/Input.h>
  9. #include <deviceToolKits/InputBase.h>
  10.  
  11. extern DTInput_t            Unit0;
  12. extern struct   Interrupt   handler_setup;
  13. extern struct Library *SysBase;
  14.  
  15. /* funcs.c */
  16.  
  17. void Error(char *string);
  18. int OpenSingleRequester(int xp, int yp);
  19. int OpenMultiRequester(int xp, int yp);
  20. void OpenStuff(void);
  21. void CloseStuff(void);
  22. int WaitForK1(void);
  23. long FileLen(FILE *fp);
  24. void LoadBank(int banknr);
  25. void SaveBank(int banknr);
  26. void SaveSound(int soundnr);
  27. void LoadSound(int soundnr);
  28. void SaveAllSounds(int banknr);
  29. void TransmitCurrentBank(int banknr);
  30. void TransmitBank(int banknr);
  31. void TransmitSingleSound(int soundnr);
  32. void GetBank(int banknr);
  33. void LoadMulti(void);
  34. void SaveMulti(void);
  35. void SendDump(void);
  36. void GetDump(void);
  37. void TransmitSingleMulti(int soundnr);
  38. void LoadDump(char *dumpname);
  39. void SaveDump(void);
  40. void Display(void);
  41. void GetMulti(void);
  42. void SendMulti(void);
  43. void ProgChange(int);
  44. void AllNotesOff(void);            
  45. void NoteOff(int note,int channel);
  46. void NoteOn(int note,int vel,int channel);
  47. BOOL Exists(char *);
  48. char *filerequester(char *dir,char *filename,char *title);
  49.  
  50. /**********************************************************************************/
  51. /***                                                                            ***/
  52. /**********************************************************************************/
  53.  
  54. void Error(char *string)
  55. {
  56.     if (ReqBase == NULL) BoolRequest(string,"Quit","Quit");
  57.                     else MyReq(string,NULL," Ok ");
  58.     CloseStuff();
  59. }
  60.  
  61. /**********************************************************************************/
  62. /***                                                                            ***/
  63. /**********************************************************************************/
  64.  
  65. int OpenSingleRequester(int xp,int yp)
  66. {
  67.     int i,select;
  68.  
  69.     if (yp+win->TopEdge-10+52 > HOEHE-10) 
  70.     {
  71.         MoveWindow(win,-(win->LeftEdge),-(win->TopEdge));
  72.         return(-1);
  73.     }
  74.     
  75.     if (!(xp<15 || xp>615 || yp<28 || yp>189))
  76.     {
  77.         if (xp>480) xp=480;
  78.         if (yp>180) yp=180;
  79.         Make_SingleReqWindow(xp-10,yp+8);
  80.         SetAPen(rp2,1);
  81.         for (i=11;i<70;i+=10)    Line(win2,1,i,win2->Width-1,i);
  82.         MyCenter(win2,9, "Edit sound");
  83.         MyCenter(win2,19,"Load sound");
  84.         MyCenter(win2,29,"Save sound");
  85.         MyCenter(win2,39,"Transmit sound");
  86.         MyCenter(win2,49,"Copy to");
  87.         MyCenter(win2,59,"Swap with");
  88.         MyCenter(win2,69,"Cancel");
  89.  
  90. Loop:    ;
  91.         WaitPort(win2->UserPort);
  92.         while(mesg=(struct IntuiMessage *) GetMsg(win2->UserPort))
  93.         {
  94.             if (mesg->Class == MOUSEBUTTONS && mesg->Code == SELECTDOWN)
  95.             {    
  96.                 select=(mesg->MouseY)/10;
  97.                 ReplyMsg((struct Message *) mesg);
  98.                 Delay(5);
  99.                 WaitPort(win2->UserPort);
  100.                 CloseWindow(win2);
  101.                 goto Ende;
  102.             }
  103.             else 
  104.             {    
  105.                 ReplyMsg((struct Message *) mesg);
  106.                 goto Loop;
  107.             }
  108.         }
  109.     }    
  110.     Ende: return(select);
  111. }
  112.  
  113. /**********************************************************************************/
  114. /***                                                                            ***/
  115. /**********************************************************************************/
  116.  
  117. int OpenMultiRequester(int xp,int yp)
  118. {
  119.     int i,select;
  120.  
  121.     if (yp+win->TopEdge-10+52 > HOEHE-10) 
  122.     {
  123.         MoveWindow(win,-(win->LeftEdge),-(win->TopEdge));
  124.         return(-1);
  125.     }
  126.     
  127.     if (!(xp<15 || xp>315 || yp<28 || yp>189))
  128.     {
  129.         if (xp>480) xp=480;
  130.         if (yp>180) yp=180;
  131.         Make_MultiReqWindow(xp-10,yp+8);
  132.         SetAPen(rp6,1);
  133.         for (i=11;i<50;i+=10)    Line(win6,1,i,win6->Width-1,i);
  134.         MyCenter(win6,9,"Edit multi");
  135.         MyCenter(win6,19,"Transmit multi");
  136.         MyCenter(win6,29,"Copy to");
  137.         MyCenter(win6,39,"Swap with");
  138.         MyCenter(win6,49,"Cancel");
  139.  
  140. Loop:    ;
  141.         WaitPort(win6->UserPort);
  142.         while(mesg=(struct IntuiMessage *) GetMsg(win6->UserPort))
  143.         {
  144.             if (mesg->Class == MOUSEBUTTONS && mesg->Code == SELECTDOWN)
  145.             {    
  146.                 select=(mesg->MouseY)/10;
  147.                 ReplyMsg((struct Message *) mesg);
  148.                 Delay(5);
  149.                 WaitPort(win6->UserPort);
  150.                 CloseWindow(win6);
  151.                 goto Ende;
  152.             }
  153.             else 
  154.             {    
  155.                 ReplyMsg((struct Message *) mesg);
  156.                 goto Loop;
  157.             }
  158.         }
  159.     }    
  160.     Ende: return(select);
  161. }
  162.  
  163. /**********************************************************************************/
  164. /***                                                                            ***/
  165. /**********************************************************************************/
  166.  
  167. void OpenStuff(void)
  168. {
  169.     IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",0);
  170.     if (IntuitionBase==NULL)     Error("Can't open intuition.library");
  171.     if (IntuitionBase.LibNode.lib_Version >=37) KICKZWO=TRUE;
  172.  
  173.     ReqBase=(struct ReqBase *)     OpenLibrary("req.library",REQVERSION);
  174.     if (ReqBase==NULL)             Error("Can't open req.library, I need version 2.5 or higher");
  175.  
  176.     GfxBase=(struct GfxBase *)     OpenLibrary("graphics.library",0);
  177.     if (GfxBase==NULL)             Error("Can't open graphics.library");
  178.  
  179.     DosBase=(struct DosBase *)     OpenLibrary("dos.library",0);
  180.     if (DosBase==NULL)             Error("Can't open dos.library");
  181.  
  182.     MidiBase=(struct MidiBase *) OpenLibrary(MIDINAME,MIDIVERSION);
  183.     if (MidiBase==NULL)         Error("Can't open midi.library");
  184.  
  185.     DiskfontBase=(struct DiskfontBase *) OpenLibrary("diskfont.library",0);
  186.     if (DiskfontBase==NULL)     Error("Can't open diskfont.library");
  187.  
  188.     IconBase=(struct IconBase *) OpenLibrary("icon.library",0);
  189.     if (IconBase==NULL)            Error("Can't open icon.library");    
  190.  
  191.     source=CreateMSource(NULL,NULL);
  192.     if (source==NULL)             Error("Can't create midi source");
  193.  
  194.     dest=CreateMDest(NULL,NULL);
  195.     if (dest==NULL)             Error("Can't create midi destination");
  196.  
  197.     inroute=MRouteDest("MidiIn",dest,&routeinfo);
  198.     if (inroute==NULL)             Error("Can't create midi route destination");
  199.  
  200.     outroute=MRouteSource(source,"MidiOut",NULL);
  201.     if (outroute == NULL)         Error("Can't create midi route source");
  202.  
  203.     if (!zz_pointer_open())        Error("Can't get sleepy pointer");
  204.  
  205.     if (SysBase->lib_Version>=37) KICKZWO=TRUE;
  206. }
  207.  
  208. /**********************************************************************************/
  209. /***                                                                            ***/
  210. /**********************************************************************************/
  211.     
  212. void CloseStuff(void)
  213. {
  214.     SetTaskPri(FindTask(NULL),0);
  215.     setenv("K1-LIB","FALSE");
  216.  
  217.     myprocess->pr_WindowPtr = olderrorwindow;
  218.         
  219.     DTInputRemHandler(Unit0,&handler_setup);
  220.     DTInputFree(Unit0);
  221.  
  222.     if (inroute != NULL)        DeleteMRoute(inroute);
  223.     if (outroute != NULL)        DeleteMRoute(outroute);
  224.     if (source != NULL)            DeleteMSource(source);
  225.     if (dest != NULL)            DeleteMDest(dest);
  226.     if (textfont != NULL)        CloseFont(textfont);
  227.  
  228.     if (win != NULL)             CloseWindow(win);
  229.     if (SCREEN != NULL)         CloseScreen(scr);
  230.     if (IntuitionBase != NULL)    CloseLibrary(IntuitionBase);
  231.     
  232.     if (GfxBase != NULL)        CloseLibrary(GfxBase);
  233.     if (MidiBase != NULL)        CloseLibrary(MidiBase);
  234.     if (DosBase != NULL)        CloseLibrary(DosBase);
  235.     if (DiskfontBase != NULL)    CloseLibrary(DiskfontBase);
  236.     if (IconBase != NULL)        CloseLibrary(IconBase);
  237.     if (ReqBase != NULL)    
  238.     {
  239.                                 PurgeFiles(&ReqStruct);
  240.                                 CloseLibrary(ReqBase);
  241.     }
  242.  
  243.     if (SCREEN == NULL)            SetOldColours();
  244.     exit(0);
  245. }
  246.  
  247. /**********************************************************************************/
  248. /***     Test ob K1 überhaupt vorhanden ist (während des Programmablaufs)       ***/
  249. /**********************************************************************************/
  250.  
  251. int  WaitForK1(void)
  252. {
  253.     unsigned char buf[5];
  254.     int i,j;
  255.  
  256.     SetTitle("Waiting....");
  257.     for (j=0;j<=1;j++)
  258.     {
  259.         for (i=0;i<=15;i++)
  260.         {
  261.             buf[0]=0xf0;    buf[1]=0x40;    buf[2]=i;
  262.             buf[3]=0x60;    buf[4]=0xf7;
  263.             PutMidiMsg(source,buf);
  264.             Delay(2);
  265.     
  266.             while(packet=GetMidiPacket(dest))
  267.             {
  268.                 if (packet->MidiMsg[OFFS_FUNC]==K1_MIA)
  269.                 {
  270.                     MASTERCHANNEL=i;
  271.                     return(i);
  272.                 }
  273.                 FreeMidiPacket(packet);
  274.             }
  275.         }
  276.     }
  277.     return(-1);
  278. }            
  279.  
  280. /**********************************************************************************/
  281. /***               Ermittelt die Länge eines bereits geöffneten Files             ***/
  282. /**********************************************************************************/
  283.  
  284. long FileLen(FILE *fp)
  285. {
  286.     long len;
  287.     
  288.     fseek(fp,0,SEEK_END);
  289.     len=ftell(fp);
  290.     fseek(fp,0,SEEK_SET);
  291.     return(len);
  292. }
  293.  
  294. /**********************************************************************************/
  295. /***                                                                            ***/
  296. /**********************************************************************************/
  297.  
  298. void LoadBank(int banknr)
  299. {
  300.     FILE *fp,*fopen();
  301.     int i,n;
  302.     long len;
  303.     unsigned char dummy;
  304.  
  305.     SetTitle("Load bank");
  306.  
  307.     strcpy(&answer[0],filerequester(bankdir,NULL,"Load bank"));    
  308.  
  309.     if (strlen(answer) != 0)
  310.     {
  311.         i=0;
  312.         fp=fopen(answer,"r");
  313.         if (fp==NULL)
  314.         {
  315.             MyReq("Can't open file",NULL," Ok ");
  316.             goto Ende;
  317.         }
  318.         len=FileLen(fp);
  319.  
  320.         if (len==2825)
  321.         {
  322.             for (i=0;i<=7;i++) dummy=fgetc(fp);        
  323.  
  324.             for (n=0;n<=31;n++)
  325.                 for (i=0;i<=87;i++)
  326.                     s[banknr][n][i]=fgetc(fp);
  327.  
  328.             fclose(fp);
  329.             strncpy(bankname[banknr],filename,20);
  330.             goto Ende;
  331.         }
  332.  
  333.         if (len==2816)
  334.         {
  335.             for (n=0;n<=31;n++)
  336.                 for (i=0;i<=87;i++)
  337.                     s[banknr][n][i]=fgetc(fp);
  338.  
  339.             fclose(fp);
  340.             strncpy(bankname[banknr],filename,20);
  341.             goto Ende;
  342.         }
  343.         
  344.         if (len==2432 || len==2441)
  345.                 MyReq("Not K1 sound bank, seems to be a multi-patch",NULL," Ok ");
  346.         else
  347.                 MyReq("This is not a K1 single sound bank !",NULL," Ok ");
  348.     }
  349. Ende: strcpy(bankdir,ReqStruct.Dir);
  350. }
  351.  
  352. /**********************************************************************************/
  353. /***                                                                            ***/
  354. /**********************************************************************************/
  355.  
  356. void SaveBank(int banknr)
  357. {
  358.     FILE *fp,*fopen();
  359.     int i,n;
  360.  
  361.     SetTitle("Save bank");
  362.  
  363.     strcpy(&answer[0],filerequester(bankdir,bankname[banknr],"Save bank"));    
  364.  
  365.     if (strlen(answer) != 0)
  366.     {
  367.         if (Exists(answer))
  368.             if (!MyReq("File exists, overwrite it ?"," Yes "," No ")) goto Ende;
  369.  
  370.         i=0;
  371.         fp=fopen(answer,"w");
  372.         if (fp==NULL)
  373.         {
  374.             MyReq("Can't create file",NULL," Ok ");
  375.             goto Ende;
  376.         }
  377.         fputc(MS_SYSEX,fp);    fputc(0x40,fp);    fputc(0x00,fp);    fputc(0x21,fp);
  378.         fputc(0x00,fp);        fputc(0x03,fp);    fputc(0x00,fp);    fputc(0x00,fp);
  379.  
  380.         for (n=0;n<=31;n++)
  381.             for (i=0;i<=87;i++)
  382.                 fputc((int)s[banknr][n][i],fp);
  383.  
  384.         fputc(MS_EOX,fp);
  385.         fclose(fp);
  386.     }
  387.     Ende: strcpy(bankdir,ReqStruct.Dir);
  388. }
  389.  
  390. /**********************************************************************************/
  391. /***                                                                            ***/
  392. /**********************************************************************************/
  393.  
  394. void SaveSound(int soundnr)
  395. {
  396.     FILE *fp,*fopen();
  397.     char name[11];
  398.     int i;
  399.  
  400.     SetTitle("Save sound");
  401.  
  402.     for (i=0;i<=9;i++) name[i]=s[soundnr/32][soundnr%32][i];
  403.     name[10]='\0';
  404.         
  405.     strcpy(&answer[0],filerequester(sounddir,name,"Save sound"));    
  406.  
  407.     if (strlen(answer) != 0)
  408.     {
  409.         if (Exists(answer))
  410.             if (!MyReq("File exists, overwrite it ?"," Yes "," No ")) goto Ende;
  411.         
  412.         fp=fopen(answer,"w");
  413.         if (fp==NULL)
  414.         {
  415.             MyReq("Can't create file",NULL," Ok ");
  416.             goto Ende;
  417.         }
  418.  
  419.         for (i=0;i<=87;i++)    fputc((int)s[soundnr/32][soundnr%32][i],fp);
  420.         fclose(fp);
  421.     }
  422.     Ende: strcpy(sounddir,ReqStruct.Dir);
  423. }
  424.  
  425. /**********************************************************************************/
  426. /***                                                                            ***/
  427. /**********************************************************************************/
  428.  
  429. void LoadSound(int soundnr)
  430. {
  431.     FILE *fp,*fopen();
  432.     int i,notdisplay;
  433.  
  434.     SetTitle("Load sound");
  435.  
  436.     if (soundnr>=1000)
  437.     {
  438.         notdisplay=1;
  439.         soundnr-=1000;
  440.     }
  441.     else notdisplay=0;
  442.  
  443.     strcpy(&answer[0],filerequester(sounddir,NULL,"Load sound"));    
  444.  
  445.     if (strlen(answer) != 0)
  446.     {
  447.         ZZ_POINTER(win);    
  448.         fp=fopen(answer,"r");
  449.         if (fp==NULL)
  450.         {
  451.             MyReq("Can't open file",NULL," Ok ");
  452.             goto Ende;
  453.         }
  454.         if (FileLen(fp) != 88)
  455.         {
  456.             MyReq("This is not a K1 sound file",NULL," Ok ");
  457.         }
  458.         else
  459.         {
  460.             for (i=0;i<=87;i++)    s[soundnr/32][soundnr%32][i]=fgetc(fp);
  461.         }
  462.         fclose(fp);
  463.     }
  464.     Ende: if (notdisplay != 1) Display();
  465.     strcpy(sounddir,ReqStruct.Dir);
  466.     CLEAR_POINTER(win);
  467. }
  468.  
  469. /**********************************************************************************/
  470. /***                                                                            ***/
  471. /**********************************************************************************/
  472.  
  473. void SaveAllSounds(int banknr)
  474. {
  475.     FILE *fp,*fopen();
  476.     char filename[255],name[11];
  477.     int i,soundnr;
  478.  
  479.     SetTitle("Save all sounds");
  480.     ZZ_POINTER(win);
  481.     for (soundnr=0;soundnr<=31;soundnr++)
  482.     {
  483.         for (i=0;i<=9;i++) name[i]=s[banknr][soundnr][i];
  484.         name[10]='\0';
  485.         strcpy(filename,"kawai:sounds/");
  486.         strcat(filename,name);
  487.  
  488.         fp=fopen(filename,"w");
  489.         if (fp==NULL) goto Ende;
  490.         
  491.         for (i=0;i<=87;i++)    fputc((int)s[banknr][soundnr][i],fp);
  492.  
  493.         fclose(fp);
  494.         Ende: ;
  495.     }
  496.     CLEAR_POINTER(win);
  497. }
  498.  
  499. /**********************************************************************************/
  500. /***                                                                            ***/
  501. /**********************************************************************************/
  502.  
  503. void TransmitCurrentBank(int banknr)
  504. {
  505.     int n,i;
  506.     unsigned char buf[97];
  507.  
  508.     ZZ_POINTER(win);
  509.     SetTitle("Transmit current bank");
  510.  
  511.     for (n=0;n<=31;n++)
  512.     {
  513.         buf[0]=MS_SYSEX; buf[1]=0x40; buf[2]=MASTERCHANNEL; buf[3]=0x20;
  514.         buf[4]=0x00; buf[5]=0x03; buf[6]=0x00; buf[7]=n+32*banknr;
  515.  
  516.         for (i=0;i<=87;i++)    buf[8+i]=s[banknr][n][i];
  517.         buf[96]=MS_EOX;
  518.         PutMidiMsg(source,buf);
  519.     }
  520.  
  521.     ProgChange(32*banknr);
  522.     CLEAR_POINTER(win);
  523. }
  524.  
  525. /**********************************************************************************/
  526. /***                                                                            ***/
  527. /**********************************************************************************/
  528.  
  529. void TransmitBank(int banknr)
  530. {
  531.     FILE *fp,*fopen();
  532.     unsigned char buf[97];
  533.     int i,n;
  534.     long len;    
  535.  
  536.     ZZ_POINTER(win);
  537.     SetTitle("Load bank and send it to K1");
  538.  
  539.     if (MyReq("        Are you shure ?\nCurrent bank will be erased !","With pleasure !","Am I crazy ?") == FALSE) goto Ende2;
  540.  
  541.     strcpy(&answer[0],filerequester(bankdir,NULL,"Transmit bank"));    
  542.  
  543.     if (strlen(answer) != 0)
  544.     {
  545.         fp=fopen(answer,"r");
  546.         if (fp==NULL)
  547.         {
  548.             MyReq("Can't open file",NULL," Ok ");
  549.             goto Ende;
  550.         }
  551.         len=FileLen(fp);        
  552.  
  553.         if (len==2825)
  554.         {
  555.             for (i=0;i<=7;i++) n=fgetc(fp);    /* Header überlesen */
  556.             for (n=0;n<=31;n++)
  557.             {
  558.                 buf[0]=MS_SYSEX; buf[1]=0x40; buf[2]=MASTERCHANNEL; buf[3]=0x20;
  559.                 buf[4]=0x00; buf[5]=0x03; buf[6]=0x00; buf[7]=n+32*banknr;
  560.     
  561.                 for (i=0;i<=87;i++)    buf[8+i]=fgetc(fp);
  562.                 buf[96]=MS_EOX;
  563.                 PutMidiMsg(source,buf);
  564.             }
  565.             fclose(fp);
  566.             goto Ende;
  567.         }
  568.         
  569.         if (len==2816)
  570.         {
  571.             for (n=0;n<=31;n++)
  572.             {
  573.                 buf[0]=MS_SYSEX; buf[1]=0x40; buf[2]=MASTERCHANNEL; buf[3]=0x20;
  574.                 buf[4]=0x00; buf[5]=0x03; buf[6]=0x00; buf[7]=n+32*banknr;
  575.     
  576.                 for (i=0;i<=87;i++)    buf[8+i]=fgetc(fp);
  577.                 buf[96]=MS_EOX;
  578.                 PutMidiMsg(source,buf);
  579.             }
  580.             fclose(fp);
  581.             goto Ende;
  582.         }
  583.  
  584.         MyReq("This is not a K1 single sound bank !",NULL," Ok ");    
  585.     }
  586.     Ende: ;
  587.     
  588.     ProgChange(32*banknr);
  589.     strcpy(bankdir,ReqStruct.Dir);
  590.     Ende2:     CLEAR_POINTER(win);
  591. }
  592.  
  593. /**********************************************************************************/
  594. /***                                                                            ***/
  595. /**********************************************************************************/
  596.  
  597. void TransmitSingleSound(int soundnr)
  598. {
  599.     int i;
  600.     unsigned char buf[97];
  601.  
  602.     buf[0]=MS_SYSEX; buf[1]=0x40; buf[2]=MASTERCHANNEL; buf[3]=0x20;
  603.     buf[4]=0x00; buf[5]=0x03; buf[6]=0x00; buf[7]=soundnr;
  604.  
  605.     for (i=0;i<=87;i++)    buf[8+i]=s[soundnr/32][soundnr%32][i];
  606.     buf[96]=MS_EOX;
  607.     PutMidiMsg(source,buf);
  608.     Delay(1);
  609.  
  610.     ProgChange(soundnr);
  611. }
  612.  
  613. /**********************************************************************************/
  614. /***                             Bank vom K1 anfordern                                ***/
  615. /**********************************************************************************/
  616.  
  617. void GetBank(int banknr)
  618. {
  619.     UBYTE *msg,buf[9],d[2600];
  620.     int i,n,fo,chn;
  621.     FILE *fp,*fopen();
  622.  
  623.     MASTERCHANNEL=chn=WaitForK1();
  624.     if (chn == -1)
  625.     {
  626.         i=MyReq("Error: K1 not present",NULL," Ok ");
  627.         goto Ende;
  628.     }
  629.  
  630.     SetTitle("Get bank from K1");
  631.  
  632. Loop: ZZ_POINTER(win);
  633.     fo=creat("ram:k1-bank",0666);
  634.     if (fo != -1)
  635.     {
  636.         buf[0]=0xf0;    buf[1]=0x40;
  637.         buf[2]=chn;     buf[3]=0x01;
  638.         buf[4]=0x00;    buf[5]=0x03;
  639.         buf[6]=0x00;    buf[7]=32*(banknr%2);
  640.         buf[8]=0xf7;
  641.         PutMidiMsg(source,buf);
  642.         Delay(3);            
  643.  
  644.         WaitPort(dest->DestPort);
  645.         if (packet = GetMidiPacket(dest)) 
  646.         {
  647.             if (write(fo,packet->MidiMsg,packet->Length) != packet->Length); 
  648.         }
  649.         close(fo);
  650.         
  651.         if (packet->Length == 2825)
  652.         {
  653.             fp=fopen("ram:k1-bank","r");
  654.             for (i=0;i<=7;i++) n=fgetc(fp);        /* Midi-Header überlesen */
  655.             for (n=0;n<=31;n++)
  656.             {
  657.                 for (i=0;i<=87;i++)    s[banknr][n][i]=fgetc(fp);
  658.             }
  659.             strcpy(bankname[banknr],"");
  660.             fclose(fp);
  661.         }
  662.         else goto Loop;
  663.         DeleteFile("ram:k1-bank");
  664.         FreeMidiPacket(packet);
  665.     }
  666.     else
  667.     {
  668.         MyReq("Can't create temporary file in RAM-Disk",NULL," Ok ");    
  669.     }
  670.     Ende: CLEAR_POINTER(win);
  671. }
  672.  
  673. /**********************************************************************************/
  674. /***                            Laden eines Multi-Patches                          ***/
  675. /**********************************************************************************/
  676.  
  677. void LoadMulti(void)
  678. {
  679.     FILE *fp,*fopen();
  680.     int i,n;
  681.     long len;
  682.  
  683.     SetTitle("Load multi-patch");
  684.  
  685.     strcpy(&answer[0],filerequester(multidir,NULL,"Load multi"));    
  686.  
  687.     if (strlen(answer) != 0)
  688.     {
  689.         i=0;
  690.         fp=fopen(answer,"r");
  691.         if (fp==NULL)
  692.         {
  693.             MyReq("Can't open file",NULL," Ok ");
  694.             goto Ende;
  695.         }
  696.         len=FileLen(fp);
  697.  
  698.         if (len==2432)
  699.         {
  700.             for (n=0;n<=31;n++)
  701.             {
  702.                 for (i=0; i<=75; i++)
  703.                 {
  704.                     m[n][i]=fgetc(fp);
  705.                 }
  706.             }
  707.             strcpy(multiname,filename);
  708.             fclose(fp);
  709.             goto Ende;
  710.         }
  711.     
  712.         if (len==2441)
  713.         {
  714.             for (i=0;i<=7;i++) n=fgetc(fp); 
  715.  
  716.             for (n=0;n<=31;n++)
  717.                 for (i=0; i<=75; i++)
  718.                     m[n][i]=fgetc(fp);
  719.  
  720.             strcpy(multiname,filename);
  721.             fclose(fp);
  722.             goto Ende;
  723.         }
  724.         
  725.         MyReq("Sorry, this is not a K1 multi-patch",NULL," Ok ");
  726.         fclose(fp);    
  727.     }
  728. Ende:     Display();
  729.     strcpy(multidir,ReqStruct.Dir);
  730. }
  731.  
  732. /**********************************************************************************/
  733. /***                    Saven eines Multi-Patches                               ***/
  734. /**********************************************************************************/
  735.  
  736. void SaveMulti(void)
  737. {
  738.     FILE *fp,*fopen();
  739.     int i,n;
  740.  
  741.     SetTitle("Save multi-patch");
  742.  
  743.     strcpy(&answer[0],filerequester(multidir,multiname,"Save multi"));    
  744.  
  745.     if (strlen(answer) != 0)
  746.     {
  747.         if (Exists(answer))
  748.             if (!MyReq("File exists, overwrite it ?"," Yes "," No ")) goto Ende;
  749.  
  750.         i=0;
  751.         fp=fopen(answer,"w");
  752.         if (fp==NULL)
  753.         {
  754.             MyReq("Can't create file",NULL," Ok ");
  755.             goto Ende;
  756.         }
  757.  
  758.         for (n=0;n<=31;n++)
  759.             for (i=0;i<=75;i++)
  760.                 fputc((int)m[n][i],fp);
  761.  
  762.         fclose(fp);
  763.     }
  764.     Ende: strcpy(multidir,ReqStruct.Dir);
  765. }
  766.  
  767. /**********************************************************************************/
  768. /***                                                                            ***/
  769. /**********************************************************************************/
  770.  
  771. void SendDump(void)
  772. {
  773.     int n,i;
  774.     unsigned char buf[90];
  775.  
  776.     SetTitle("Send dump to K1");
  777.  
  778.     TransmitCurrentBank(0);
  779.     TransmitCurrentBank(1);
  780.     SendMulti();
  781. }
  782.  
  783. /**********************************************************************************/
  784. /***                     Multipatch von K1 holen                                 ***/
  785. /**********************************************************************************/
  786.  
  787. void GetDump(void)
  788. {
  789.     SetTitle("Get dump from K1");
  790.     
  791.     GetBank(0); Delay(2);
  792.     GetBank(1); Delay(2);
  793.     GetMulti(); Delay(2);
  794. }
  795.  
  796. /**********************************************************************************/
  797. /***                                                                            ***/
  798. /**********************************************************************************/
  799.  
  800. void TransmitSingleMulti(int soundnr)
  801. {
  802.     int i;
  803.     unsigned char buf[97];
  804.  
  805.     buf[0]=MS_SYSEX; buf[1]=0x40; buf[2]=MASTERCHANNEL; buf[3]=0x20;
  806.     buf[4]=0x00; buf[5]=0x03; buf[6]=0x00; buf[7]=64+soundnr;
  807.  
  808.     for (i=0;i<=75;i++)    buf[8+i]=m[soundnr][i];
  809.     buf[84]=MS_EOX;
  810.     PutMidiMsg(source,buf);
  811.     Delay(2);
  812.  
  813.     ProgChange(64+soundnr);
  814. }
  815.  
  816. /**********************************************************************************/
  817. /***                                                                            ***/
  818. /**********************************************************************************/
  819.  
  820. void LoadDump(char *dumpname)
  821. {
  822.     FILE *fp,*fopen();
  823.     int i,n;
  824.     long len;
  825.     unsigned char dummy;
  826.     char string[255];
  827.  
  828.     SetTitle("Load dump");
  829.  
  830.     if (dumpname != NULL)
  831.     {
  832.         strcpy(string,dumpdir);
  833.         strcat(string,dumpname);
  834.         strcpy(answer,string);
  835.         goto Load;
  836.     }
  837.  
  838.     strcpy(answer,filerequester(dumpdir,NULL,"Load dump"));    
  839.  
  840.     if (strlen(answer) != 0)
  841.     {
  842. Load:    fp=fopen(answer,"r");
  843.         len=FileLen(fp);
  844.  
  845.         if (len==8082)
  846.         {
  847.             for (n=0;n<=31;n++)                        /* 1. Bank lesen */            
  848.                 for (i=0;i<=87;i++)
  849.                     s[0][n][i]=fgetc(fp);
  850.  
  851.             for (n=0;n<=31;n++)                     /* 1. Bank lesen */            
  852.                 for (i=0;i<=87;i++)
  853.                     s[1][n][i]=fgetc(fp);
  854.             
  855.             for (i=0;i<=17;i++) n=fgetc(fp);        /* Lücke überlesen */
  856.  
  857.             for (n=0;n<=31;n++)
  858.                 for (i=0;i<=75;i++)
  859.                     m[n][i]=fgetc(fp);
  860.  
  861.             fclose(fp);
  862.             goto Ende;
  863.         }
  864.         else MyReq("This is not a K1 dump file",NULL," Ok ");    
  865.     }
  866.  
  867. Ende: Display();
  868.     strcpy(bankname[0],"");
  869.     strcpy(bankname[1],"");
  870.     strcpy(multiname,"");
  871.     if (ReqStruct.Dir) strcpy(dumpdir,ReqStruct.Dir);
  872. }
  873.  
  874. /**********************************************************************************/
  875. /***                                                                            ***/
  876. /**********************************************************    ************************/
  877.  
  878. void SaveDump(void)
  879. {
  880.     FILE *fp,*fopen();
  881.     int i,n;
  882.  
  883.     SetTitle("Save dump");
  884.  
  885.     strcpy(&answer[0],filerequester(dumpdir,NULL,"Save dump"));    
  886.  
  887.     if (strlen(answer) != 0)
  888.     {
  889.         if (Exists(answer))
  890.             if (!MyReq("File exists, overwrite it ?"," Yes "," No ")) goto Ende;
  891.  
  892.         i=0;
  893.         fp=fopen(answer,"w");
  894.         if (fp==NULL)
  895.         {
  896.             ("Can't create file");
  897.             goto Ende;
  898.         }
  899.  
  900.         for (n=0;n<=31;n++)                        /* 1. Bank */
  901.             for (i=0;i<=87;i++)
  902.                 fputc((int)s[0][n][i],fp);
  903.  
  904.         for (n=0;n<=31;n++)                        /* 2. Bank */
  905.             for (i=0;i<=87;i++)
  906.                 fputc((int)s[1][n][i],fp);
  907.  
  908.         for (i=0;i<=17;i++) fputc((int)0,fp);    /* Lücke auffüllen */
  909.  
  910.         for (n=0;n<=31;n++)                        /* Multi-Bank */
  911.             for (i=0;i<=75;i++)
  912.                 fputc((int)m[n][i],fp);
  913.  
  914.         fclose(fp);
  915.     }
  916.     Ende: 
  917.     if (ReqStruct.Dir) strcpy(dumpdir,ReqStruct.Dir);
  918. }
  919.  
  920.  
  921. /**********************************************************************************/
  922. /***                                                                            ***/
  923. /**********************************************************************************/
  924.  
  925. void Display(void)
  926. {
  927.     if (multi==0) DisplayBank();
  928.             else  DisplayMulti();
  929. }
  930.  
  931. /**********************************************************************************/
  932. /***                                                                            ***/
  933. /**********************************************************************************/
  934.  
  935. void GetMulti(void)
  936. {
  937.     UBYTE *msg,buf[9],d[2600];
  938.     int i,n,fo,chn;
  939.     FILE *fp,*fopen();
  940.  
  941.     ZZ_POINTER(win);
  942.     MASTERCHANNEL=chn=WaitForK1();    
  943.     if (chn == -1)
  944.     {
  945.         i=MyReq("Error: K1 not present",NULL," Ok ");
  946.         goto Ende;
  947.     }
  948.  
  949.     SetTitle("Get multi-patch from K1");
  950.  
  951. Loop: ;
  952.     fo=creat("ram:k1-bank",0666);
  953.     if (fo != -1)
  954.     {
  955.         buf[0]=0xf0;    buf[1]=0x40;
  956.         buf[2]=chn;        buf[3]=0x01;
  957.         buf[4]=0x00;    buf[5]=0x03;
  958.         buf[6]=0x00;    buf[7]=64;
  959.         buf[8]=0xf7;
  960.         PutMidiMsg(source,buf);
  961.         Delay(5);
  962.  
  963.         WaitPort(dest->DestPort);
  964.         if (packet = GetMidiPacket(dest)) 
  965.         {
  966.             if (write(fo,packet->MidiMsg,packet->Length) != packet->Length); 
  967.         }
  968.         close(fo);
  969.         
  970.         if (packet->Length == 2441)
  971.         {
  972.             fp=fopen("ram:k1-bank","r");
  973.             for (i=0;i<=7;i++) n=fgetc(fp);        /* Midi-Header überlesen */
  974.  
  975.             for (n=0;n<=31;n++)
  976.                 for (i=0;i<=75;i++)
  977.                     m[n][i]=fgetc(fp);
  978.  
  979.             strcpy(multiname,"");
  980.             fclose(fp);
  981.         }
  982.         else goto Loop;
  983.         DeleteFile("ram:k1-bank");
  984.         FreeMidiPacket(packet);
  985.     }
  986.     else
  987.     {
  988.         MyReq("Can't create temporary file in RAM-Disk",NULL," Ok ");    
  989.     }
  990.     Ende: CLEAR_POINTER(win);
  991. }
  992.  
  993. /**********************************************************************************/
  994. /***                                                                            ***/
  995. /**********************************************************************************/
  996.  
  997. void SendMulti(void)
  998. {
  999.     int i,n;
  1000.     unsigned char buf[97];
  1001.  
  1002.     SetTitle("Send multi-patch to K1");
  1003.  
  1004.     ZZ_POINTER(win);
  1005.     for (n=0;n<=31;n++) 
  1006.     {
  1007.         buf[0]=MS_SYSEX; buf[1]=0x40; buf[2]=MASTERCHANNEL; buf[3]=0x20;
  1008.         buf[4]=0x00; buf[5]=0x03; buf[6]=0x00; buf[7]=64+n;
  1009.  
  1010.         for (i=0;i<=75;i++)    buf[8+i]=m[n][i];
  1011.         buf[84]=MS_EOX;
  1012.         PutMidiMsg(source,buf);
  1013.         Delay(1);
  1014.     }
  1015.     Delay(5);
  1016.  
  1017.     ProgChange(64);
  1018.  
  1019.     CLEAR_POINTER(win);
  1020. }
  1021.  
  1022. /**********************************************************************************/
  1023. /***                                                                            ***/
  1024. /**********************************************************************************/
  1025.  
  1026. void ProgChange(int nr)
  1027. {
  1028.     unsigned char buf[2];
  1029.  
  1030.     if (nr>=64 && nr<96)
  1031.     {
  1032.         buf[0]=192+MASTERCHANNEL;
  1033.         buf[1]=nr-1;
  1034.         PutMidiMsg(source,buf);
  1035.         Delay(2);    
  1036.     }
  1037.  
  1038.     if (nr<64)
  1039.     {
  1040.         buf[0]=192+MASTERCHANNEL;
  1041.         buf[1]=nr+1;
  1042.         PutMidiMsg(source,buf);
  1043.         Delay(2);    
  1044.     }
  1045.  
  1046.     buf[0]=192+MASTERCHANNEL;
  1047.     buf[1]=nr;
  1048.     PutMidiMsg(source,buf);
  1049.     Delay(2);    
  1050. }
  1051.  
  1052. /**********************************************************************************/
  1053. /***                                                                            ***/
  1054. /**********************************************************************************/
  1055.  
  1056. void NoteOn(int note,int vel,int channel)
  1057. {
  1058.     UBYTE buf[3];
  1059.  
  1060.     buf[0] = MS_NOTEON | channel;
  1061.     buf[1] = note;
  1062.     buf[2] = vel;
  1063.     PutMidiMsg(source,buf);
  1064. }
  1065.  
  1066. /**********************************************************************************/
  1067. /***                                                                            ***/
  1068. /**********************************************************************************/
  1069.  
  1070. void NoteOff(int note,int channel)        
  1071. {    
  1072.     UBYTE buf[3];
  1073.  
  1074.     buf[0] = MS_NOTEOFF | channel;
  1075.     buf[1] = note;
  1076.     buf[2] = 64;
  1077.     PutMidiMsg(source,buf);
  1078. }
  1079.  
  1080. /**********************************************************************************/
  1081. /***                                                                            ***/
  1082. /**********************************************************************************/
  1083.  
  1084. void AllNotesOff(void)            
  1085. {
  1086.     UBYTE buf[3];
  1087.  
  1088.     buf[0] = MS_MODE | MASTERCHANNEL;
  1089.     buf[1] = MM_ALLOFF;        
  1090.     buf[2] = 0;
  1091.     PutMidiMsg(source,buf);    
  1092. }
  1093.  
  1094. /**********************************************************************************/
  1095. /***                                                                            ***/
  1096. /**********************************************************************************/
  1097.  
  1098. BOOL Exists(char *file)
  1099. {
  1100.     struct Lock *lock;
  1101.  
  1102.     lock=Lock(file,ACCESS_READ);
  1103.     if (lock==NULL)     return(FALSE);
  1104.     UnLock(lock);
  1105.     return(TRUE);    
  1106. }
  1107.  
  1108. /**********************************************************************************/
  1109. /***                                                                            ***/
  1110. /**********************************************************************************/
  1111.  
  1112. void Unkown(void)
  1113. {
  1114.     char str[]={0x44,0x65,0x64,0x69,0x63,0x61,0x74,0x65,0x64,0x20,0x74,0x6f,0x20,0x44,0x6f,0x72,0x6f,0x74,0x68,0x65,0x65,00};
  1115. }
  1116.  
  1117. /**********************************************************************************/
  1118. /***                                                                            ***/
  1119. /**********************************************************************************/
  1120.  
  1121. char *filerequester(char *dir,char *filename,char *title)
  1122. {
  1123.     char answer[255];    
  1124.     
  1125.     if (dir==NULL)         dir        =(char *)"";
  1126.     if (filename==NULL) filename=(char *)"";
  1127.  
  1128.     answer[0] = 0;
  1129.  
  1130.     ReqStruct.PathName         = answer;
  1131.     ReqStruct.Dir             = dir;
  1132.     ReqStruct.File             = filename;
  1133.     ReqStruct.Title         = title;
  1134.     ReqStruct.VersionNumber =REQVERSION;
  1135.     ReqStruct.Flags         = FRQCACHINGM+(SCREEN != NULL ? FRQNODRAGM : NULL);
  1136.  
  1137.     if (FileRequester(&ReqStruct)) return(&answer[0]);
  1138.                             else   return((char *)"");
  1139. }